import { ApiUrl } from './ApiUrl'

const baseUrl = ApiUrl.wapUrl
const apiPath = ''

export const request = async (option) => {
  const header = {}
	let cookie = uni.getStorageSync('cookie')
	if (cookie) {
		header.cookie = cookie
	}
	uni.showLoading({
		title: "数据加载中"
	})
  const res = await uni.request({
		header: {
			...header, ...option.header
		},
    withCredentials: true,
    url: baseUrl + apiPath + option.url,
    method: option.method || 'GET',
    data: option.data,
  })
	uni.hideLoading();
	if (res.cookies.length) {
	  const cookie = res.cookies[0].split('; ')[0]
		uni.setStorageSync('cookie', cookie)
	}
	if (!res.data.isSuccess && res.data.msg && res.data.msg.includes("当前会话已过期,请重新登录")) {
		setTimeout(() => {
			return uni.navigateTo({
				url: "/pages/login/index"
			})
		}, 500)
	}
  if ((!res.data.code && !res.data.isSuccess) || (!res.data.isSuccess && res.data.code !== 200)) {
		if ((!res.data.code && !res.data.isSuccess)) {
			return uni.showToast({ title: res.data.msg || "", icon: 'none' })
		} else {
			return uni.showToast({ title: res.data.message || "", icon: 'none' })
		}
  }
  return res.data
}